package com.gasstion.shiro;

import com.gasstion.dao.RoleDao;
import com.gasstion.dao.UserinfoDao;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class MyRealm extends AuthorizingRealm {
    @Autowired
    private UserinfoDao userInfoDao ;
    @Autowired
    private RoleDao roleDao ;
    //授权

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken ;
        String username = token.getUsername() ;
        //1.根据用户名从数据库表中获取用户的密码
        String pwd = userInfoDao.getPwdByUName(username) ;
        if(pwd==null || "".equals(pwd)){
            return  null ;
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username,pwd,"myrealm") ;
        return info;
    }

}

